import team from "../../services/api.js"
import utils from "../../utils/util.js"
const app = getApp()

Page({
    data: {
        list: {},
        startTime: '',
        endTime: '',
        loading: false
    },

    onShareAppMessage() {
        return {
            title: 'NBA赛程查询',
            path: '/pages/index/index',
            imageUrl: 'https://energymonster.oss-cn-shanghai.aliyuncs.com/upload_img/nba.png'
        }
    },

    onLoad() {
        let startTime = utils.formatTime(new Date()),
            endTime = utils.computeDate(startTime);

        startTime = startTime.split(' ')[0];
        endTime = endTime.split(' ')[0];

        this.setData({
            startTime,
            endTime
        })
        this.getGameList();
        // this.getSQLTeam();
    },

    /**
     * 获取赛程
     */
    getGameList() {
        this.setData({
            loading: true
        })
        let params = {
            startTime: this.data.startTime,
            endTime: this.data.endTime
        }
        team.getTencentGame(params).then(res => {
            console.info(res)
            if (res.code == 200) {
                let list = res.data
                Object.keys(list).forEach((key) => {
                    if (Array.isArray(list[key])) {
                        list[key].forEach((ret) => {
                            ret['start_time'] = ret.startTime.split(' ')[1].split(':')[0] + ':' + ret.startTime.split(' ')[1].split(':')[1]
                        })
                    }
                })
                list = Object.assign(this.data.list, list)
                this.setData({
                    list
                })
            }
            this.setData({
                loading: false
            })
        })
    },
    getSQLTeam() {
        team.getTeamList().then(res => {
            console.info(res)
        })
    },
    onReachBottom() {
        if (!this.data.loading) {
            let startTime = this.data.endTime,
                endTime = utils.computeDate(this.data.endTime).split(' ')[0]
            this.setData({
                startTime,
                endTime
            })
            this.getGameList()
        }
    }
})